//
//  MessageThankOtherViewController.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/19.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "MessageThankOtherViewController.h"
#import "Common.h"
#import "MessageCommonTableViewCell.h"
#import "BGMUser.h"
#import "BGMHTTP.h"
#import <NIMSDK/NIMSDK.h>

#import "MessagePayViewController.h"
#import "SVProgressHUD.h"
@interface MessageThankOtherViewController ()<UITableViewDelegate,UITableViewDataSource,MessageCommonTableViewCellDelegate>

@property (nonatomic, strong)UITableView *myTable;

@property (nonatomic, strong)UIButton *ThanksBtn;

@property (nonatomic, strong)NSMutableArray *allUsers; //所有要w答谢的好友
@property (nonatomic, strong)NSArray *ThanksUsers; //所有要w答谢的好友
@property (nonatomic, strong)NSMutableArray *seleUser; //要w答谢的好友

@property (nonatomic, assign)int row; //答谢成功好友

@property (nonatomic, strong)UIButton *goBtn;
@end

@implementation MessageThankOtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+70;
    self.titleBtn.frame = rect;
    [self.titleBtn setTitle:@"帮忙完成" forState:UIControlStateNormal];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(payResultThanks) name:@"pay_money_thanks" object:nil];;
    
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(15, self.navview.frame.size.height, SCREENW-15, 50)];
    label.text = @"您的帮忙已经完成，是否选择好友进行答谢？";
    label.textColor = bgmmainColor;
    [self.view addSubview:label];
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(15, label.frame.origin.y+label.frame.size.height, SCREENW-15, 0.3)];
    line.backgroundColor = [UIColor grayColor];
    line.alpha = 0.3;
    [self.view addSubview:line];
 
    self.allUsers = [[NSMutableArray alloc]init];
    self.seleUser = [[NSMutableArray alloc]init];
    
    self.myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, line.frame.origin.y+1, SCREENW, SCREENH-line.frame.origin.y-80)];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTable];
    
     for(int i =0;i<self.selegroupArray.count;i++){
        [self getFetchMemberWithIndex:i completion:^(BOOL finished) {
            if (finished) {
                NSSet *set = [NSSet setWithArray:self.allUsers];
                self.ThanksUsers = [set allObjects];
                for (NIMUser *user in self.ThanksUsers) {
                    [self.seleUser addObject:@"1"];
                }
                [self.myTable reloadData];
            }
        }];
    }
    
    
    self.goBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, SCREENH-70, SCREENW-60, 40)];
    self.goBtn.layer.masksToBounds = YES;
    self.goBtn.layer.cornerRadius = 8;
    [self.goBtn setTitle:@"跳过" forState:UIControlStateNormal];
    [self.goBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.goBtn.backgroundColor = bgmgrayColor;

    [self.view addSubview:self.goBtn];
    [self.goBtn addTarget:self action:@selector(clickThank:) forControlEvents:UIControlEventTouchUpInside];
 
}
- (void)clickThank:(UIButton *)btn{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)getFetchMemberWithIndex:(NSInteger)index completion:(void(^)(BOOL finished))completed{
    
    if(index>=0 && index<[self.selegroupArray count]){
        NIMTeam *m = self.selegroupArray[index];
        [[[NIMSDK sharedSDK]teamManager]fetchTeamMembers:m.teamId completion:^(NSError * _Nullable error, NSArray<NIMTeamMember *> * _Nullable members) {
            
            for (NIMTeamMember *member in members) {
                NIMUser *user = [[[NIMSDK sharedSDK]userManager] userInfo:member.userId];
                if(![user.userId isEqualToString:[BGMUser shareInstance].userId]){
                    [self.allUsers addObject:user];
                }
                
            }

            if(index==[self.selegroupArray count]-1){
                if(completed){
                    completed(YES);
                }
                
            }else{
                [self getFetchMemberWithIndex:index+1 completion:completed];
            }
            
            
            
        }];
        
    }
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.ThanksUsers.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 61;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    lable.text = @"   选择打赏";
    lable.textColor = bgmgrayColor;
    return lable;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identfier = @"MessageCommonTableViewCell";
    MessageCommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
    if (cell == nil) {
        cell = [[MessageCommonTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.seleBtn.hidden = NO;
        cell.cellDelegate = self;
        
    }
    cell.isThankUser = YES;
    cell.seleBtn.tag = (int)indexPath.row;
    BOOL b = NO;
    NSString *sele = [self.seleUser objectAtIndex:indexPath.row];
    if ([sele isEqualToString:@"1"]) {
        b = YES;
    }else{
        b = NO;
    }
    [cell setUserInfo:[self.ThanksUsers objectAtIndex:indexPath.row] sele:b];
    
    return cell;
}

#pragma mark -- 答谢好友 --
-(void)gotoThankUser:(int)tag{
    
    self.row = tag;
    NIMUser *user =[self.allUsers objectAtIndex:tag];
    MessagePayViewController *vc = [[MessagePayViewController alloc]init];
    vc.type = BGMThanksRed;
    vc.thankUserID = user.userId;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)payResultThanks{
    [SVProgressHUD showSuccessWithStatus:@"答谢完成"];
    
    [self.seleUser replaceObjectAtIndex:self.row withObject:@"0"];
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:self.row inSection:0];
    [self.myTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

}

-(void)seleRow:(int)row isSele:(BOOL)isSele{
    if (isSele){
        [self.seleUser replaceObjectAtIndex:row withObject:@"1"];
    }else{
        [self.seleUser replaceObjectAtIndex:row withObject:@"0"];
    }
     NSIndexPath *indexPath=[NSIndexPath indexPathForRow:row inSection:0];
     [self.myTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark -----
- (void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
